package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.service.FormConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.domain.entity.FormConfigEntity;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.form.pojo.FormConfigPoJo;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.form.pojo.dto.FormConfigGetMultiDTO;
import com.xbongbong.pro.form.pojo.dto.FormConfigSetDTO;
import com.xbongbong.pro.form.pojo.vo.FormConfigMultiGetVO;
import com.xbongbong.pro.form.pojo.vo.FormConfigSetVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/5/22 11:04
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/form/config")
public class FormConfigController {

    @Resource
    private FormConfigService formConfigService;

    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid FormConfigSetDTO formConfigSetDTO, BindingResult br) {
        XbbResponse<FormConfigSetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormConfigSetVO formConfigSetVO = formConfigService.enable(formConfigSetDTO);
                response = new XbbResponse<>(formConfigSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/getButtonByFormId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getButtonByFormId(@RequestBody @Valid FormConfigGetMultiDTO formConfigGetMultiDTO, BindingResult br){
        XbbResponse<FormConfigMultiGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormConfigMultiGetVO formConfigGetMultiVO = formConfigService.getButtonByFormId(formConfigGetMultiDTO);
                response = new XbbResponse<>(formConfigGetMultiVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }

        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/getProductDataSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getProductDataSet(@RequestBody @Valid FormConfigGetMultiDTO formConfigGetMultiDTO, BindingResult br){
        XbbResponse<FormConfigEntity> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormConfigEntity formConfigEntity = formConfigService.getProductDataSet(formConfigGetMultiDTO);
                response = new XbbResponse<>(formConfigEntity);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }

        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/saveProductDataSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveProductDataSet(@RequestBody @Valid FormConfigSetDTO formConfigSetDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = formConfigService.saveProductDataSet(formConfigSetDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }

        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
